package de.koller.million.card.engine.player;

import de.koller.million.card.CNode;
import de.koller.oceanFramework.util.MIn;
import de.koller.oceanFramework.util.MOut;
import java.util.HashSet;
import java.util.Set;

public class PlayerContainer extends CNode {
	
	public final Set<Player> players = new HashSet<>();
	
	private double highestDamage = 0;
	private int highestProgress = 1;

	@Override
	protected void init() {
		super.init();
		
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
	}
	
	public void save( MOut o ) {
		o.writeInt( players.size() );
		for( Player p : players ) {
			o.writeInt( p.id );
			p.save( o );
		}
	}
	
	public void load( MIn i ) {
		int a = i.readInt( 1 )[0];
		for( int j = 0; j < a; j++ ) {
			int id = i.readInt( 1 )[0];
			Player p = createNewPlayer( id );
			p.load( i );
		}
			
	}

	public Player createNewPlayer( int id ) {
		Player p;
		if( id != -1 )
			p = new Player( id );
		else
			p = new Player( croot.generatePlayerID() );
		this.addChild( p );
		this.croot.mainFrame.playerBoxModel.setChanged();
		return p;
	}
	
	public void deletePlayer( Player p ) {
		p.removeMe();
		this.croot.mainFrame.playerBoxModel.setChanged();
	}

	public void newDamageEquipt( Player p ) {
		if( p.stats.getDamage() > highestDamage ) {
			highestDamage = p.stats.getDamage();
//			croot.news.publish( new TextNewsEntry( croot.getTime(), 
//					"Neuer Rekord: " +
//					p.toString() + " erreichte Schadenswert: " + Math.round( highestDamage ) ) );
		}
	}
	
}
